package com.skcc.cip.ngms.converter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.camel.Exchange;
import org.apache.camel.util.ObjectHelper;

import com.skcc.cip.component.exchange.ExchangeConst;
import com.skcc.cip.component.exchange.dto._2007.MailBoxListRequest;
import com.skcc.cip.component.exchange.dto._2007.MailContactListRequest;
import com.skcc.cip.component.exchange.dto._2007.MailContentRequest;
import com.skcc.cip.component.exchange.dto._2007.MailConvertIdRequest;
import com.skcc.cip.component.exchange.dto._2007.MailDeleteRequest;
import com.skcc.cip.component.exchange.dto._2007.MailGetAttachmentRequest;
import com.skcc.cip.component.exchange.dto._2007.MailListRequest;
import com.skcc.cip.component.exchange.dto._2007.MailSaveAndSendRequest;
import com.skcc.cip.component.exchange.dto._2007.MailUpdateItemRequest;
import com.skcc.cip.component.exchange.dto._2007.PushSubscribeRequest;
import com.skcc.cip.component.exchange.dto._2007.ResolveNamesRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.attachment.CommonMailAttachmentRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.contact.CommonMailContactListRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.content.CommonMailContentRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.convertid.CommonMailConvertIdRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.del.CommonMailDelRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.isread.CommonIsReadRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.list.CommonMailListRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.resolve.CommonResolveNamesRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mail.send.CommonMailSendRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mailbox.CommonMailBoxListRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mailpush.CommonMailPushSubscribeRequest;
import com.skcc.cip.ngms.dto.request.jaxb.mailpush.CommonMailPushUnsubscribeRequest;

public class MailTransform {
	
//	public static Map<String, String> subscribeList = new HashMap<String, String>();
	private String pushListenerUrl = "http://10.250.26.138:9898/notification";
//	private String pushEventType = "NewMailEvent";
//	private String pushFolderId = "inbox";
	
	public MailBoxListRequest transformMailBoxRequest(Exchange exchange, CommonMailBoxListRequest input) {

		MailBoxListRequest returnDto = new MailBoxListRequest();
		 
		returnDto.setOperationName("MAIL_BOX_LIST");

		return returnDto;

	}

	public PushSubscribeRequest transformMailPushRequest(Exchange exchange, CommonMailPushSubscribeRequest input) {
		PushSubscribeRequest dto = null;
		dto = new PushSubscribeRequest();
		dto.setOperationName("PUSH_SUBSCRIBE");
		dto.setEventType(input.getPushEventType());
		dto.setFolderId(input.getPushFolderId());
		dto.setReturnUrl(pushListenerUrl);
		dto.setStatusFrequency(input.getStatusFrequency());
		
		exchange.setProperty("subscribeInfo", input);

		return dto;
	}

	public void transformMailPushUnsubscribe(Exchange exchange, CommonMailPushUnsubscribeRequest input) {
//		Map<String, Object> headers = exchange.getIn().getHeaders();
//		String user = (String)headers.get("authUser");
//		if(MailTransform.subscribeList.containsValue(user)){
//			 Iterator<String> iter = MailTransform.subscribeList.keySet().iterator();
//			 while(iter.hasNext()){
//				 String key = iter.next();
//				 if(MailTransform.subscribeList.get(key).equals(user)){
//					 MailTransform.subscribeList.remove(key);
//				 }
//			 }
//		}
	}
	
	public MailListRequest transformMailListRequest(CommonMailListRequest input) {
        MailListRequest returnDto = new MailListRequest();

        returnDto.setOperationName("MAIL_LIST");
//        returnDto.setChangeKey(input.getCHANGEKEY());
//        returnDto.setItemId(input.getID());

        if (input.getSearchType() != null) {
            returnDto.setSearchType(input.getSearchType());
            returnDto.setSearchWord(input.getSearchWord());
        }

        /*
         * @XmlElement private String mailBoxType; // 편지함 구분 (I: 받은편지함, S:
         * 보낸편지함, T: 임시보관함, D: // 휴지통,F: 사용자폴더)
         */
        String type = "inbox";
        if (input.getMailBoxType().equals("D")) {
            type = "deleteditems";
        } else if (input.getMailBoxType().equals("S")) {
            type = "outbox";
        }

        if(input.getMailBoxType().equals("F")){
        	returnDto.setParentFolderIdsType(ExchangeConst.PARENT_FOLDER_IDS_TYPE_ITEMID);
        }else{
        	returnDto.setParentFolderIdsType(ExchangeConst.PARENT_FOLDER_IDS_TYPE_DISTINGUISHEDID);
        }
        returnDto.setChangeKey(input.getChangeKey());
        returnDto.setItemId(input.getId());
        returnDto.setDistinguishedId(type);
        returnDto.setPage(input.getPage() - 1);
        returnDto.setCountPerPage(input.getCountPerPage());
        returnDto.setBaseShape(ExchangeConst.BASE_SHAPE_ALL);
        return returnDto;
    }
	
	public MailContentRequest transformMailContentRequest(CommonMailContentRequest input) {
        MailContentRequest returnDto = new MailContentRequest();

        returnDto.setOperationName("MAIL_CONTENT");
        returnDto.setChangeKey(input.getCHANGEKEY());
        returnDto.setItemId(input.getID());
        returnDto.setBodyType(input.getContentBodyType());
        returnDto.setAttachmentInclude(input.getAttachmentInclude());
        returnDto.setBaseShape(ExchangeConst.BASE_SHAPE_ALL);
        return returnDto;
    }
	
	 public MailGetAttachmentRequest transformMailAttachmentRequest(CommonMailAttachmentRequest input) {
	        MailGetAttachmentRequest dto = new MailGetAttachmentRequest();
	        
	        dto.setOperationName("MAIL_ATTACHMENT");
	        dto.setAttachmentId(input.getAttachId());
	        return dto;
	    }
	
	 public MailContactListRequest transformMailContactListRequest(CommonMailContactListRequest input) {
	        MailContactListRequest returnDto = new MailContactListRequest();

	        returnDto.setOperationName("MAIL_CONTACT_LIST");

	        returnDto.setCountPerPage(Integer.parseInt(input.getCountPerPage()));
	        returnDto.setPage(Integer.parseInt(input.getPage()) - 1);
	        returnDto.setBaseShape(ExchangeConst.BASE_SHAPE_ALL);
	        if (ObjectHelper.isNotEmpty(input.getSearchWord())) {
	            returnDto.setSearchType(input.getSearchType());
	            returnDto.setSearchWord(input.getSearchWord());
	        }

	        return returnDto;
	 }
	 
	 public ResolveNamesRequest transformResolveNamesRequest(CommonResolveNamesRequest input) {
		 ResolveNamesRequest returnDto = new ResolveNamesRequest();

	        returnDto.setOperationName("RESOLVE_NAMES");

	        returnDto.setUnResolveEntry(input.getUnresolvedEntry());

	        return returnDto;
	 }
	
	public MailUpdateItemRequest transformMailIsReadRequest(CommonIsReadRequest input) {
	        MailUpdateItemRequest returnDto = new MailUpdateItemRequest();

	        returnDto.setOperationName("MAIL_ISREAD");

	        returnDto.setItemId(input.getID());
	        returnDto.setChangeKey(input.getCHANGEKEY());
	        returnDto.setChangedData(input.getISREAD());
	        returnDto.setFieldURI("message:IsRead");
	        returnDto.setMessageDisposition(ExchangeConst.MESSAGE_DISPOSITION_SAVEONLY);

	        return returnDto;
	}
	  
	public MailSaveAndSendRequest transformMailSendRequest(CommonMailSendRequest input) {
        MailSaveAndSendRequest returnDto = new MailSaveAndSendRequest();

        returnDto.setOperationName("MAIL_SAVE_AND_SEND");

        returnDto.setBody(input.getBODY());
        returnDto.setSubject(input.getSUBJECT());
        returnDto.setBodyType(input.getContentBodyType());
        returnDto.setSendType(input.getSendType());
        returnDto.setOrgID(input.getID());
        returnDto.setOrgCHANGEKEY(input.getCHANGEKEY());
        returnDto.setTo(input.getTo());
        returnDto.setCc(input.getCc());
        returnDto.setBcc(input.getBcc());
        returnDto.setMessageDisposition(ExchangeConst.MESSAGE_DISPOSITION_SENDANDSAVECOPY);

        return returnDto;
    }
	
	
	 public MailDeleteRequest transformMailDelRequest(CommonMailDelRequest input) {
	        MailDeleteRequest returnDto = new MailDeleteRequest();

	        returnDto.setOperationName("MAIL_DELETE");
	        
	        returnDto.setItemId(input.getID());
	        returnDto.setChangeKey(input.getCHANGEKEY());
	        if ("true".equals(input.getIsHardDel())) {
	            returnDto.setDeleteType(ExchangeConst.DELETE_ITEE_TYPE.HARD_DELETE.toString());
	        } else {
	            returnDto.setDeleteType(ExchangeConst.DELETE_ITEE_TYPE.MOVE_TO_DELETED_ITEMS.toString());
	        }
	        return returnDto;
	    }
	 
	 public MailConvertIdRequest transformMailConvertIdRequest(CommonMailConvertIdRequest input){
		 MailConvertIdRequest returnDto = new MailConvertIdRequest();
		 
		 returnDto.setOperationName("MAIL_CONVERT_ID");
		 
		 returnDto.setDestinationFormat(input.getDestinationFormat());
		 returnDto.setFolderId(input.getFolderId());
		 returnDto.setItemId(input.getItemId());
		 
		 return returnDto;
	 }
	
}
